package com.fg.estate.controller;

import com.fg.estate.bean.Permission;
import com.fg.estate.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * @author 丰哥
 * 2022/10/10
 * 权限的控制器
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    // 显示权限列表画面
    @RequestMapping("/listUi")
    public String listUi(ModelMap map) {

        try {
            List<Permission> permissions = permissionService.selectAll();
            map.addAttribute("permissions", permissions);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "permission_list";
    }

    // 显示添加权限画面
    @RequestMapping("/addUi")
    public String addUi() {
        return "permission_add";
    }

    // 添加权限
    @RequestMapping("/add")
    public String add(Permission permission) {
        permission.setCreateTime(new Date());
        try {
            permissionService.insert(permission);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "permission_add";
    }

    // 删除
    @RequestMapping("/delete")
    public String delete(long id) {
        try {
            permissionService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/permission/listUi";
    }

    // 编辑画面
    @RequestMapping("/editUi")
    public String editUi(long id, ModelMap map) {
        try {
            Permission permission = permissionService.selectById(id);
            map.addAttribute("permission", permission);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "permission_edit";
    }

    // 编辑画面
    @RequestMapping("/edit")
    public String edit(Permission permission) {
        try {
            permissionService.update(permission);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "permission_edit";
    }

    // 跳转显示权限不足的画面
    @RequestMapping("/nopermission")
    public String nopermission() {
        return "no_permission";
    }
}
